package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
/*import com.itheima.dao.MenuDao;*/
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
/*import com.itheima.service.MenuService;*/
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;

    //分页查询
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<Role> page = roleDao.findAll(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public Role findById(Integer id) {
        Role role = roleDao.findById(id);
        return role;
    }

    @Override
    public void edit(Role role) {

        roleDao.update(role);
        roleDao.deleteMenu(role.getId());
        roleDao.deletePermission(role.getId());
        this.addId(role);
    }
    @Override
    public List<Integer> permissionIdList(Integer id) {
        List<Integer> permissionIdList = roleDao.permissionIdList(id);
        return permissionIdList;
    }


    @Override
    public Result delete(Integer id) {
        List<Integer> menuIdList = roleDao.menuIdList(id);
        List<Integer> permissionIdList = roleDao.permissionIdList(id);
        List<Integer> userIdList = roleDao.userIdList(id);
        if((menuIdList==null&&permissionIdList==null&&userIdList==null)||(menuIdList.size()==0&&permissionIdList.size()==0&&userIdList.size()==0)){
            roleDao.deleteRole(id);
            return new Result(true,"删除成功");
        }else{
            return new Result(false,"删除失败,有关联");
        }
    }

    @Override
    public List<Menu> findAllMenu() {
        List<Menu> menuList = roleDao.findAllMenu();
        return menuList;
    }

    @Override
    public List<Integer> menuIdList(Integer id) {
        List<Integer> menuIdList = roleDao.menuIdList(id);
        return menuIdList;
    }


    public void addId(Role role) {
        Integer[] permissionIds = role.getPermissionIds();
        for (Integer permissionId : permissionIds) {
            addRolePermissionId(role.getId(),permissionId);
        }
        Integer[] menuIds = role.getMenuIds();
        for (Integer menuId : menuIds) {
            addRoleMenuId(role.getId(),menuId);
        }
    }
    @Override
    public void add(Role role) {
        roleDao.add(role);
        Integer[] permissionIds = role.getPermissionIds();
        for (Integer permissionId : permissionIds) {
            addRolePermissionId(role.getId(),permissionId);
        }
        Integer[] menuIds = role.getMenuIds();
        for (Integer menuId : menuIds) {
            addRoleMenuId(role.getId(),menuId);
        }
    }

    public void addRoleMenuId(Integer roleId,Integer menuId){
        Map<String,Integer> map = new HashMap<>();
        map.put("role_id",roleId);
        map.put("menu_id",menuId);
        roleDao.addRoleMenuId(map);
    }
    public void addRolePermissionId(Integer roleId,Integer permissionId){
        Map<String,Integer> map = new HashMap<>();
        map.put("role_id",roleId);
        map.put("permission_id",permissionId);
        roleDao.addRolePermissionId(map);
    }
}
